package com.jdjw.modules.busfarmersapply.controller;


import com.google.common.collect.Lists;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.busassetarchives.service.BusAssetArchivesService;
import com.jdjw.modules.busfarmersapply.entity.BusFarmersApply;
import com.jdjw.modules.busfarmersapply.entity.BusFarmersApplySave;
import com.jdjw.modules.busfarmersapply.entity.BusFarmersApplyVo;
import com.jdjw.modules.busfarmersapply.service.BusFarmersApplyService;
import com.jdjw.modules.busgeneralapply.entity.BusGeneralApply;
import com.jdjw.modules.busgeneralapply.entity.BusGeneralApplyCommit;
import com.jdjw.modules.wfactivity.entity.WfActivity;
import com.jdjw.modules.wfactivity.service.WfActivityService;
import com.jdjw.modules.wftask.entity.WfTask;
import com.jdjw.modules.wftask.service.WfTaskService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 农户重大疾病困难补助 前端控制器
 * </p>
 *
 * @author fq
 * @since 2021-07-23
 */
@RestController
@RequestMapping("/busfarmersapply")
public class BusFarmersApplyController {

    @Autowired
    private BusFarmersApplyService busFarmersApplyService;


    @Autowired
    protected WfActivityService wfActivityService;

    @Autowired
    private WfTaskService wfTaskService;

    @ApiOperation("农户重大疾病困难补助-查询")
    @PostMapping("queryAll")
    public ResponseResult queryAll(BusFarmersApplyVo busFarmersApplyVo){
        return busFarmersApplyService.queryAll(busFarmersApplyVo);
    }

    @ApiOperation("农户重大疾病困难补助-添加")
    @PostMapping("save")
    public ResponseResult save(BusFarmersApplySave busFarmersApplySave){
        ResponseResult rrt=new ResponseResult();
        try {
            BusFarmersApply busFarmersApply=new BusFarmersApply();
            //转换
            BeanUtils.copyProperties(busFarmersApplySave,busFarmersApply);
            busFarmersApply.setCreateDate(new Date());
            busFarmersApply.setOrgCode(busFarmersApplySave.getDeptId());
            busFarmersApply.setPassFlag("0");
            busFarmersApplyService.save(busFarmersApply);
            wfActivityService.createWorkFlow("NHZDJB", busFarmersApplySave.getDeptId(),busFarmersApply.getId(),busFarmersApplySave.getCreateBy(),busFarmersApplySave.getUsername());
        rrt.setCode(200);
        rrt.setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setCode(500);
            rrt.setMsg("保存失败"+e.getMessage());
        }
        return rrt;
    }

    @ApiOperation("农户重大疾病困难补助-编辑")
    @PostMapping("edit")
    public ResponseResult edit(@RequestBody String sqlEdit) throws JSONException {
        JSONObject json=new JSONObject(sqlEdit);
       return busFarmersApplyService.sqlEdit(json.getString("sqlEdit"));
    }


    @ApiOperation("农户重大疾病困难补助-删除")
    @PostMapping("delete")
    public ResponseResult delete(String id){
        ResponseResult rrt=new ResponseResult();
        try {
            busFarmersApplyService.removeById(id);
            rrt.setCode(200);
            rrt.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setCode(500);
            rrt.setMsg("删除失败"+e.getMessage());
        }
        return rrt;
    }

    @ApiOperation("农户重大疾病困难补助-查看")
    @PostMapping("{id}/preview")
    public ResponseResult preview(@PathVariable("id") String id){
        ResponseResult rrt=new ResponseResult();
        try {
            BusFarmersApply apply = busFarmersApplyService.getById(id);
            Map<String,Object> map=new HashMap<>();
            map.put("apply_id", apply.getId());
            List<WfActivity> wfActivities = (List<WfActivity>) wfActivityService.listByMap(map);
            List<WfTask> taskList = Lists.newArrayList();
            if (wfActivities != null && wfActivities.size() > 0) {
                taskList = wfTaskService.queryByactivityId(wfActivities.get(0).getId());
                apply.setTaskList(taskList);
            }
            String nodeCode = wfActivityService.findNodeCode(apply.getId());
            apply.setNodeCode(nodeCode);
            rrt.setCode(200);
            rrt.setMsg("查看成功");
            rrt.setData(apply);
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setCode(500);
            rrt.setMsg("查看失败"+e.getMessage());
            rrt.setData(null);
        }

        return rrt;
    }

    @ApiOperation("农户重大疾病困难补助-提交")
    @PostMapping("commit")
    public ResponseResult commit(@RequestBody BusGeneralApplyCommit busGeneralApplyCommit){
        ResponseResult rrt=new ResponseResult();

        try {
            String deptId2 = busGeneralApplyCommit.getDeptId();
            String nodeCode = wfActivityService.findNodeCode(busGeneralApplyCommit.getId());

            WfTask wfTask = busGeneralApplyCommit.getWfTask();
            if (wfTask == null) {
                wfTask = new WfTask();
            }
            wfTask.setAttIds(busGeneralApplyCommit.getWfTask().getAttIds());
            String dualOpinion = StringUtils.isBlank(wfTask.getDualOpinion()) ? "01" : wfTask.getDualOpinion();
            BusFarmersApply busApply1 = busFarmersApplyService.getById(busGeneralApplyCommit.getId());
            if ("01".equals(dualOpinion)) {  // 同意
                // 判断是否是特殊节点
//                String[] userIdS = new String[]{busApply1.getCreateBy()};
                if("XZSH".equals(nodeCode)){
                    busApply1.setPassFlag("2");
                    busFarmersApplyService.updateById(busApply1);
                    wfActivityService.sendNextNodeAllPerson(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, wfTask);
                } else {
                    wfActivityService.sendNextNodeAllPerson(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, wfTask);
                }
            } else if ("02".equals(dualOpinion)) { // 不同意
                busApply1.setPassFlag("1");
                busFarmersApplyService.updateById(busApply1);
                wfActivityService.sendAppointNodeAllPerson(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, "GD", wfTask);
            } else if ("04".equals(dualOpinion)) { // 退回
                wfActivityService.sendBack(busGeneralApplyCommit.getId(),busGeneralApplyCommit.getUserId(),busGeneralApplyCommit.getUsername(), deptId2, wfTask);
            }
            rrt.setMsg("提交成功");
            rrt.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setMsg("提交失败"+e.getMessage());
            rrt.setCode(200);
        }
        return rrt;
    }



}

